/*
 ============================================================================
 Name        : YuebaoFramework.cpp
 Author      :
 Version     :
 Copyright   :
 Description : CMagazineFramework implementation
 ============================================================================
 */
#include "fWml.h"
#include "IapEngine.h"
#include "Downloader.h"
#include "MagazineCanvasManager.h"
#include "MagazineFramework.h"

#include "MagazineDefines.h"
#include "HeraMessageBox.h"

#include "fFile.h"

#include "YueBaoData.h"
#include "fImageFactory.h"
#include "ImageDatabase.h"
#include "YuebaoMagazineAppView.h"
#include "ProjectsDefines.h"
#include "YuebaoStrings.h"
#include "YuebaoServerDefines.h"
#include "fEditor.h"

YuebaoFramework::YuebaoFramework( ) :
    fEngine( )
{
    // No implementation required
}

void YuebaoFramework::ConstructL( RWsSession & aWs, CWsScreenDevice & aScreenDevice, RWindowBase & aWindow, CCoeControl * aView )
{
    DebugReset();
    iDefaultScreenSize = TSize( kDefaultScreenWidth, kDefaultScreenHeight );
    fEngine::ConstructL( aWs, aScreenDevice, aWindow, aView );
    _LIT( kMagazineDirectory, "c:\\yuebao\\" );
    iMagazineResource.Copy( kMagazineDirectory );
    iMagazineResource.Des( )[ 0 ] = ResourcePath( )[ 0 ];
}

void YuebaoFramework::InitPreEnter( )
{
    fEngine::InitPreEnter( );
    
    iIsSecondInitalizeFinshed = EFalse;
    
    SecondInitalize( );
    
    _yueBaoData = CYueBaoData::NewL( this );
}

YuebaoFramework::~YuebaoFramework( )
{
    /*
     TInt aoError = 0;
     TInt delay = 0;
     while( delay < 0xfff )
     {
     if( CActiveScheduler::Current( )->RunIfReady( aoError, CActive::EPriorityIdle ) )
     {
     User::WaitForAnyRequest( );
     }
     CEikonEnv::Static( )->Flush( );
     delay++;
     }*/

    SAFE_DELETE( _yueBaoData );
    
    SAFE_DELETE( iLoginDaemon );
    
    SAFE_DELETE( iIapEngine );
    SAFE_DELETE( iSplashTxt );
    SAFE_DELETE( _recommend_sms_content );
    SAFE_DELETE( _share_sms_content );
    SAFE_DELETE( iHMessageBox );
    
    iInputIndicators.Reset();
}

YuebaoFramework *YuebaoFramework::NewLC( RWsSession & aWs, CWsScreenDevice & aScreenDevice, RWindowBase & aWindow, CCoeControl * aView )
{
    YuebaoFramework *self = new ( ELeave ) YuebaoFramework( );
    
    CleanupStack::PushL( self );
    self->ConstructL( aWs, aScreenDevice, aWindow, aView );
    return self;
}

YuebaoFramework *YuebaoFramework::NewL( RWsSession & aWs, CWsScreenDevice & aScreenDevice, RWindowBase & aWindow, CCoeControl * aView )
{
    YuebaoFramework *self = YuebaoFramework::NewLC( aWs, aScreenDevice, aWindow, aView );
    
    CleanupStack::Pop( ); // self;
    return self;
}

fCanvasManager *YuebaoFramework::CreateCanvasManager( )
{
    return YuebaoCanvasManager::NewL( this );
}

/*
 HttpDownloadEngine *YuebaoFramework::GetDownLoader()
 {
 return iDownLoader;
 }
 */

fIapEngine *YuebaoFramework::GetIapEngine( )
{
    return iIapEngine;
}

/*SpecialNetworkForBrowse *YuebaoFramework::GetNetworkEngine()
 {
 return iNetEngineForWebBrowse;

 }*/

/*CDomBuilder *YuebaoFramework::GetDomBuilder()
 {
 if( iDom == NULL )
 {
 iDom = new ( ELeave ) CDomBuilder;
 }

 return iDom;
 }*/

TBool YuebaoFramework::IsOnline( )
{
    return true;
    //return GetIapEngine( )->IsOnLine( );
}

void YuebaoFramework::ShowLoadingBox( MDialogEventObserver* aObserver, const TDesC& aTitle, const TDesC& aMessage, const TDesC& left, const TDesC& right, const TDesC& /*center*/)
{
    if( iHMessageBox )
    {
        iHMessageBox->ResetKeyEvent( );
        iHMessageBox->SetObserver( aObserver );
        iHMessageBox->SetImagePosition( HeraMessageBox::EImageAtTop );
        iHMessageBox->SetKeys( left, right );
        iHMessageBox->Show( aTitle, aMessage );
    }
}

void YuebaoFramework::ShowMessageBox( MDialogEventObserver* aObserver, const TDesC& aTitle, const TDesC& aMessage, const TDesC& left, const TDesC& right, const TDesC& /*center*/)
{
    if( iHMessageBox )
    {
        iHMessageBox->ResetKeyEvent( );
        iHMessageBox->SetObserver( aObserver );
        iHMessageBox->SetImagePosition( HeraMessageBox::EImageNone );
        iHMessageBox->SetKeys( left, right );
        iHMessageBox->Show( aTitle, aMessage );
    }
}

void YuebaoFramework::HideLoadingBox( )
{
    if( iHMessageBox )
    {
        iHMessageBox->SetObserver( NULL );
        iHMessageBox->MakeVisible( EFalse );
    }
}

void YuebaoFramework::DialogHide( fDialog* dialog )
{
    if( dialog == iCurrentDlg && iCurrentDlg != iHMessageBox )
    {
        iCurrentDlg = NULL;
    }
}
void YuebaoFramework::HideMessageBox( )
{
    if( iHMessageBox )
    {
        iHMessageBox->SetObserver( NULL );
        iHMessageBox->MakeVisible( EFalse );
    }
}

TBool YuebaoFramework::IsMessageBoxShow( )
{
    if( iHMessageBox )
    {
        return iHMessageBox->IsVisible( );
    }
    else
    {
        return EFalse;
    }
}

TBool YuebaoFramework::IsLoadingBoxShow( )
{
    if( iHMessageBox )
    {
        return iHMessageBox->IsVisible( );
    }
    else
    {
        return EFalse;
    }
}

fControlBase *YuebaoFramework::GetCurrentDialog( )
{
    if( iCurrentDlg && iCurrentDlg->IsVisible( ) )
    {
        return iCurrentDlg;
    }
    else
    {
        return NULL;
    }
}

void YuebaoFramework::CreateDialog( )
{
    iHMessageBox = HeraMessageBox::NewL( this );
    iHMessageBox->SetKeys( kConfirm, kCancel );
    iHMessageBox->SetDialogColor( kCanvasBackgroundColor, kCanvasBackgroundColor, kCanvasBackgroundColor );
    iHMessageBox->SetTextViewColor( kCanvasBackgroundColor );
}

void YuebaoFramework::SecondInitalize( )
{
    if( iIsSecondInitalizeFinshed )
    {
        return;
    }

    TFileName iapConfig( DataPath( ) );
    iapConfig.Append( kIapConfig );
    iIapEngine = new fIapEngine( iapConfig, EFalse );
    
    iLoginDaemon = LoginDaemon::NewL( this );
    
    iSplashTxt = NULL;
    
    ( ( CYuebaoMagazineAppView* )iInstance )->iIsEngineInited = ETrue;
    
    iIsSecondInitalizeFinshed = ETrue;
}

CEikEdwin* YuebaoFramework::NewEdwin( )
{
    CYuebaoMagazineAppView *instance = ( CYuebaoMagazineAppView * )iInstance;
    return instance->NewEdwin( );
}
CEikSecretEditor *YuebaoFramework::NewSecretEditor( )
{
    CYuebaoMagazineAppView *instance = ( CYuebaoMagazineAppView * )iInstance;
    return instance->NewSecretEditor( );
}

CEikEdwin* YuebaoFramework::NewMultilineEdwin( TInt aMaxLength, TInt aLineWidth, TInt aLines )
{
    CYuebaoMagazineAppView *instance = ( CYuebaoMagazineAppView * )iInstance;
    return instance->NewMultilineEdwin( aMaxLength, aLineWidth, aLines );
}

void YuebaoFramework::DestoryEdwin( CEikEdwin* pEikEdwinToBeDestory )
{
    CYuebaoMagazineAppView *instance = ( CYuebaoMagazineAppView * )iInstance;
    instance->DestoryEdwin( pEikEdwinToBeDestory );
}

void YuebaoFramework::DestoryEdwin( CEikSecretEditor* pEikEdwinToBeDestory )
{
    CYuebaoMagazineAppView *instance = ( CYuebaoMagazineAppView * )iInstance;
    instance->DestoryEdwin( pEikEdwinToBeDestory );
}

void YuebaoFramework::SetFontHeight( CEikEdwin* pEditor, int nFontHeightInPixel )
{
    CYuebaoMagazineAppView *instance = ( CYuebaoMagazineAppView * )iInstance;
    instance->SetFontHeight( pEditor, nFontHeightInPixel );
}

void YuebaoFramework::HideAllEditor( )
{
    CYuebaoMagazineAppView *instance = ( CYuebaoMagazineAppView * )iInstance;
    instance->HideAllEditor( );
}

void YuebaoFramework::DrawInputIndicator( TRect aRect )
{
    TFepType type = fEditor::GetCurrentIndicatorState( );
    if( type != EFepTypeUnknown )
    {
        int index = ( int )type;
        
        if( iInputIndicators.Count( ) == 0 )
        {
            iInputIndicators.AppendL( ApplyResourceImage( _L("input1.png") ) );
            iInputIndicators.AppendL( ApplyResourceImage( _L("input2.png") ) );
            iInputIndicators.AppendL( ApplyResourceImage( _L("input3.png") ) );
            iInputIndicators.AppendL( ApplyResourceImage( _L("input4.png") ) );
            iInputIndicators.AppendL( ApplyResourceImage( _L("input5.png") ) );
            iInputIndicators.AppendL( ApplyResourceImage( _L("input6.png") ) );
            
        }
        if( index < 0 || index > iInputIndicators.Count( ) - 1 )
        {
            return;
        }

        fImage *image = iInputIndicators[ index ];
        
        if( image )
        {
            iGfxEngine->DrawBitBlt( aRect.iBr.iX - image->Width( ), aRect.iTl.iY - image->Height( ), image );
        }
    }
}
//end of file
